package com.ng.framework.view.desktop;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class NGGUIUtils {
	
	private static final int DEFAULT_TEXT_BUTTON_HEIGHT = 25;
	private static final int DEFAULT_TEXT_BUTTON_WIDTH = 70;
	private static final int DEFAULT_ICON_BUTTON_HEIGHT = 25;
	private static final int DEFAULT_ICON_BUTTON_WIDTH = 25;
	
	public static JButton createTextButton(final String text, final ActionListener actionListener) {
		final JButton button = new JButton(text);
		
		button.setPreferredSize(new Dimension(Math.max(button.getPreferredSize().width, DEFAULT_TEXT_BUTTON_WIDTH), DEFAULT_TEXT_BUTTON_HEIGHT));
		button.setMinimumSize(button.getPreferredSize());
		button.addActionListener(actionListener);
		
		return button;
	}
	
	public static JButton createIconButton(final String icon, final ActionListener actionListener) {
		final JButton button = new JButton(NGGUIUtils.getImageIcon(icon));
		
		button.setPreferredSize(new Dimension(DEFAULT_ICON_BUTTON_WIDTH, DEFAULT_ICON_BUTTON_HEIGHT));
		button.addActionListener(actionListener);
		
		return button;
	}
	
	public static ImageIcon getImageIcon(final String icon) {
		return new ImageIcon(ClassLoader.getSystemClassLoader().getResource(icon));
	}
	
	public static Window findWindow(Component c) {
		if (c == null) {
			return JOptionPane.getRootFrame();
		} else if (c instanceof Window) {
			return (Window) c;
		} else {
			return findWindow(c.getParent());
		}
	}
	
	public static void centerWindow(Window window) {
	    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
	    int x = (int) ((dimension.getWidth() - window.getWidth()) / 2);
	    int y = (int) ((dimension.getHeight() - window.getHeight()) / 2);
	    window.setLocation(x, y);
	}

}
